<?php

if (!defined('NV_SYSTEM')) { die ("You can't access this file directly..."); }

require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
if (file_exists("".$datafold."/config_".$module_name.".php")) {
	@require_once ("".$datafold."/config_".$module_name.".php");
}
if (defined('_MODTITLE')) $module_title = _MODTITLE;
############################################

if(defined('IS_ADMMOD') || defined('IS_USER')) {
	if(isset($_GET['ad_id']) & !isset($_POST['verified'])){
			$ad_id = $_GET['ad_id'];
			echo "<center>";
			echo "<form name =\"sendmail\" action =\"#\" method = post>";
			echo _DIACHINHAN;
			echo "<br>";
			echo "<input type =\"text\" name = \"mailaddr\" size = 30>";
			echo "<input type =\"hidden\" name = \"ad_id\" value =$ad_id>";
			echo "<input type =\"hidden\" name = \"verified\" value =\"no\">";
			echo "<br>";
			echo "<input type =\"submit\" name = \"submit\" value =\"Submit\">";
			echo "</form>";
			echo "</center>";
			unset($_GET['ad_id']);
	}
	// perform email checking
	if(isset($_POST['verified']) & $_POST['verified']=='no' ){
		$mailaddr = $_POST['mailaddr'];
		
		if (!preg_match('#^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$#',$mailaddr)){
			echo "<center>";
			echo _EMAILKHONGHOPLE.$mailaddr;
			echo "<br><a href=\"javascript:history.go(-1)\">Go back</a>";
			echo "</center>";
		}else{
			echo "<center>";
			echo "<form name =\"sendmail\" action =\"#\" method = post>";
			echo "<b>"._XACNHAN._DIACHINHAN."</b>";
			echo "<br>";
			echo "<input type =\"hidden\" name = \"mailaddr\" size = 30>";
			echo "<input type =\"hidden\" name = \"ad_id\" value =$ad_id>";
			echo "<input type =\"hidden\" name = \"verified\" value =\"yes\">";
			echo "<br>";
			echo $mailaddr;
			echo "<br><br>";
			echo "<input type =\"button\" name = \"back\" value =\"<< Back\" onclick =\"javascrip:history.go(-1)\" >";
			echo "<input type =\"submit\" name = \"submit\" value =\"Submit >>\">";
			echo "</form>";
			echo "</center>";
		}
	}
	if(isset($_POST['ad_id']) & $_POST['verified']=='yes' ){
		
		$a_id = intval($_POST['ad_id']);
    $sql = "SELECT * FROM ad_item WHERE item_id='$a_id'";
    $result = $db->sql_query($sql);
    if ($numrows = $db->sql_numrows($result) != 1) { exit("Sorry, there is an error."); }
	  $row = $db->sql_fetchrow($result);
				
		$user_agent = $_SERVER["HTTP_USER_AGENT"];
		$user_hostip = $_SERVER["REMOTE_ADDR"];
		$user_hostname = gethostbyaddr($user_hostip);
		$user_time = date("Y/m/d H:i:s ");
		$to = $_POST['mailaddr']; //mail@ookyaku.com
		$subject = $row['title'];
		$message = $row['content'];
		//echo $message;
		//$message = wordwrap($message, 70);

		// To send HTML mail, the Content-type header must be set
		$headers  = 'MIME-Version: 1.0' . "\r\n";
		$headers .= 'Content-type: text; charset=utf-8' . "\r\n"; 
		$headers .= "From: Rao Vat Website" . "\r\n";	
		
				
		if(mail($to, $subject, $message, $headers))	{
			echo("The information has been sent.");
		} else	{
			echo "\n Send mail is failure. Please contact administrator.";		
		}
		echo "<br><a href=\"\" onclick=\"window.close();\">CLOSE</a>";
	}
}

?>